<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="90px"
    >
      <el-form-item label="元数据名称" prop="metaName">
        <el-input
          v-model="queryParams.metaName"
          class="!w-240px"
          clearable
          placeholder="请输入元数据名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="元数据标识" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          class="!w-240px"
          clearable
          placeholder="请输入元数据标识"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button
          v-hasPermi="['system:role:create']"
          plain
          type="primary"
          @click="openForm('create')"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
        <el-button
          v-hasPermi="['system:role:export']"
          :loading="exportLoading"
          plain
          type="success"
          @click="handleExport"
        >
          <Icon class="mr-5px" icon="ep:download" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="编号" prop="id" />
      <el-table-column align="center" label="元数据名称" prop="metaName" />
      <el-table-column align="center" label="元数据标识" prop="metaCode" />
      <el-table-column align="center" label="元数据表单" prop="tableForm" v-if="false" />
      <el-table-column align="center" label="备注" prop="remark" />
      <el-table-column
        :formatter="dateFormatter"
        align="center"
        label="创建时间"
        prop="createTime"
        width="180"
      />
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button
            v-hasPermi="['system:role:update']"
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            v-hasPermi="['system:role:update']"
            link
            type="primary"
            @click="openViewer(scope.row.tableForm, scope.row.metaName)"
          >
            预览
          </el-button>
          <el-button
            v-hasPermi="['system:role:delete']"
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <div
    v-if="showPreviewDialogFlag"
    class=""
    v-drag="['.drag-dialog.el-dialog', '.drag-dialog .el-dialog__header']"
  >
    <el-dialog
      v-model="showPreviewDialogFlag"
      :title="previewTitle"
      :show-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center
      :destroy-on-close="true"
      :append-to-body="true"
      class="drag-dialog small-padding-dialog"
      width="75%"
      align-center
    >
      <div>
        <div class="form-render-wrapper">
          <v-form-render :form-json="formJson" :form-data="formData" :option-data="optionData" />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as MetaApi from '@/api/meta/index'
import { VFORM_URL } from '@/utils/config'
import { ref, reactive } from 'vue'

defineOptions({ name: 'MetaAdmin' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  metaName: '',
  tableName: '',
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const showPreviewDialogFlag = ref(false)

/* 注意：formJson是指表单设计器导出的json，此处演示的formJson只是一个空白表单json！！ */
let formJson = {
  widgetList: [],
  formConfig: {}
}
const formData = reactive({})
const optionData = reactive({})
let previewTitle = ''

/** 查询角色列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MetaApi.page(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
// const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (type == 'create') {
    window.open('/vform/index', '_blank')
  } else if (type == 'update') {
    window.open('/vform/index?id=' + id, '_blank')
  }
}
const openViewer = (form: string, title: string) => {
  previewTitle = title
  let f = JSON.parse(form)
  formJson.widgetList = f.widgetList
  formJson.formConfig = f.formConfig
  console.log(formJson)
  showPreviewDialogFlag.value = true
}

/** 数据权限操作 */
const dataPermissionFormRef = ref()
const openDataPermissionForm = async (row: MetaApi.RoleVO) => {
  dataPermissionFormRef.value.open(row)
}

/** 菜单权限操作 */
const assignMenuFormRef = ref()
const openAssignMenuForm = async (row: MetaApi.RoleVO) => {
  assignMenuFormRef.value.open(row)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    //await MetaApi.deleteRole(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    //const data = await MetaApi.exportRole(queryParams)
    //download.excel(data, '角色列表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.body {
  margin: 0;
}
</style>
